package com.tronsis.distribution.activity.me.stock;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.tronsis.distribution.R;
import com.tronsis.distribution.bean.MyStockBean;
import com.tronsis.distribution.bean.UserBean;
import com.tronsis.distribution.biz.factory.OrderBizFactory;
import com.tronsis.distribution.biz.factory.UserBizFactory;
import com.tronsis.distribution.http.Url;
import com.tronsis.library.activity.ToolBarBaseActivity;
import com.tronsis.library.adapter.recyclerview.CommonAdapter;
import com.tronsis.library.adapter.recyclerview.MultiItemTypeAdapter;
import com.tronsis.library.adapter.recyclerview.base.ViewHolder;
import com.tronsis.library.adapter.recyclerview.wrapper.LoadMoreWrapper;
import com.tronsis.library.http.ListBean;
import com.tronsis.library.http.UICallBack;
import com.tronsis.library.util.DisplayUtil;
import com.tronsis.library.util.ToastUtil;
import com.tronsis.library.widget.MyDecoration;
import com.tronsis.library.widget.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 库存
 */
public class StockActivity extends ToolBarBaseActivity {

    @BindView(R.id.rv_stock)
    RecyclerView mRvStock;
    @BindView(R.id.tv_buy_limit)
    TextView mTvBuyLimit;
    @BindView(R.id.btn_delivery)
    Button mBtnDelivery;

    private int mPage = 1;
    private int mSize = 15;
    private int mTotal;
    private ArrayList<MyStockBean> mMyStockList = new ArrayList<>();
    private LoadMoreWrapper mLoadMoreWrapper;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stock);
        ButterKnife.bind(this);
        setTitle(true, getString(R.string.stock), getString(R.string.stock_detail));

        setView();

        initRv();

        httpGetStockList(true,mPage, mSize);
        httpGetUserInfo();
    }

    private void setView() {
        UserBean loginUser = UserBizFactory.createBiz().getLoginUser(mContext);
//        if (StringUtil.isEquals(loginUser.getRole(), StateUtil.ROLE_UO_AGENT)) {
//            mTvBuyLimit.setVisibility(View.VISIBLE);
//            mTvBuyLimit.setText(getString(R.string.format_buy_limit, loginUser.getBuyLimit()));
//        } else {
        mTvBuyLimit.setVisibility(View.GONE);
//        }
    }

    private void initRv() {
        CommonAdapter<MyStockBean> adapter = new CommonAdapter<MyStockBean>(mContext, mMyStockList, R.layout.item_stock) {
            @Override
            protected void convert(ViewHolder holder, MyStockBean stockBean, int position) {
                holder.setImageByUrl(mContext, R.id.iv_cover, Url.FILE_DOWNLOAD + stockBean.getCoverFid());
                holder.setText(R.id.tv_product_name, stockBean.getName());
                holder.setText(R.id.tv_amount, "x" + stockBean.getAmount());
            }
        };

        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                // TODO: 2019/4/14  是否要详情
//                Intent intent = new Intent(mContext, GoodsDetailActivity.class);
//                startActivity(intent);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });


        mLoadMoreWrapper = new LoadMoreWrapper(adapter);
        mLoadMoreWrapper.setLoadMoreView(R.layout.rv_load_more);
        mLoadMoreWrapper.setOnLoadMoreListener(new LoadMoreWrapper.OnLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                if (mMyStockList.size() % 15 == 0 && mMyStockList.size() > 0 && mMyStockList.size() < mTotal) {
                    mPage = (mMyStockList.size() / 15) + 1;
                    httpGetStockList(false,mPage, mSize);
                }
            }
        });
        int height = DisplayUtil.getInstance(mContext).dp2px(1);
        int color = Color.parseColor("#F4F4F4");
        RecycleViewDivider decoration = new RecycleViewDivider(mContext, MyDecoration.VERTICAL_LIST, height, color);
        mRvStock.addItemDecoration(decoration);
        mRvStock.setLayoutManager(new LinearLayoutManager(mContext));
        mRvStock.setAdapter(mLoadMoreWrapper);
    }

    @Override
    protected void onToolbarClicked(int paramId) {
        super.onToolbarClicked(paramId);
        switch (paramId) {
            case RIGHT_TEXT:
                Intent intent = new Intent(mContext, StockLogActivity.class);
                startActivity(intent);
                break;
        }
    }

    @OnClick(R.id.btn_delivery)
    void deliverClick() {
        if (mMyStockList == null || mMyStockList.isEmpty()) {
            ToastUtil.showMessage(mContext, "暂无库存");
            return;
        }
        Intent intent = new Intent(mContext, StockDeliverActivity.class);
        intent.putExtra("MyStockList", mMyStockList);
        startActivity(intent);
    }

    private void httpGetStockList(final boolean isRefresh, int page, int size) {
        try {
            OrderBizFactory.createBiz().getMyStockList(this, page, size, new UICallBack() {
                @Override
                public void onStart() {
                    showLoadingDialog();
                }

                @Override
                public void onRefresh(Object data) {
                    dismissLoadingDialog();
                    ListBean<MyStockBean> listBean = (ListBean<MyStockBean>) data;
                    mTotal = listBean.getTotal();
                    List<MyStockBean> list = listBean.getList();

                    if (isRefresh) {
                        mMyStockList.clear();
                    }

                    for (MyStockBean myStockBean : list) {
                        if (myStockBean.getAmount() > 0) {
                            mMyStockList.add(myStockBean);
                        }
                    }

                    if (!mMyStockList.isEmpty()) {
                        mBtnDelivery.setVisibility(View.VISIBLE);
                        mTvRight.setVisibility(View.VISIBLE);
                    }
                    mLoadMoreWrapper.notifyDataSetChanged();
                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            dismissLoadingDialog();
        }
    }

    private void httpGetUserInfo() {
        try {
            UserBizFactory.createBiz().getUserInfo(this, new UICallBack() {
                @Override
                public void onStart() {

                }

                @Override
                public void onRefresh(Object data) {
                    UserBean userBean = (UserBean) data;
                    setView();
                }

                @Override
                public void onFailure(int status) {

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
